package org.lixf.thread;

class Windows implements Runnable {

    private int count = 5000;// 小于500看不出效果

    // 同步代码块
    // 同步监视器：锁 任何一个类的对象都可以充当锁 多个线程必须共用一把锁

    // 同步方法
    // 非静态方法：锁是this
    // 静态方法：  锁是XXX.class
    Object object = new Object();

    @Override
    public void run() {

        while (true) {
            synchronized (object) {
                if (count > 0) {
//                    try {
//                        Thread.sleep(10);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    System.out.println(Thread.currentThread().getName() + " count = " + count);
                    count--;
                } else {
                    break;
                }
            }
        }
    }
}

public class WindowsTest {
    public static void main(String[] args) {
        Windows windows = new Windows();
        Thread t1 = new Thread(windows);
        Thread t2 = new Thread(windows);
        Thread t3 = new Thread(windows);

        t1.setName("窗口一");
        t2.setName("窗口二");
        t3.setName("窗口三");

        t1.start();
        t2.start();
        t3.start();
    }
}
